﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Server.Net;
using Server.Core;
using Server.Data;
using Server.Settings;


namespace Server
{
    
    class Program
    {
        
        static int Main(string[] args)
        {
                      
            ServerSettings _serverSettings = new ServerSettings();
            ServerNet _serverNet;
            String input, command, param;
            Int32 val, port = 0;
            
           // Console.WriteLine(_serverSettings.Port);
            _serverNet = new ServerNet(_serverSettings.Port);
            
          //  _serverNet.OpenConnection(_serverSettings.Port);
            while (true)
            {
                
                input = Console.ReadLine();
                try
                {
                    val = input.IndexOf(" ");
                    command = input.Substring(0, val);
                }
                catch (Exception)
                {
                    command = input;
                }
                
                switch (command)
                {
                    default:
                        Console.WriteLine("Invalid command. Type help");
                        break;
                    case "help":
                        Console.WriteLine("You can find more help if you type:");
                        Console.WriteLine("\t configure help");
                        Console.WriteLine("\t server help");
                        Console.WriteLine("\t debug help");
                        break;
                    case "exit":
                        _serverNet.DisconnectEvent();
                        _serverNet.CloseConnection();
                        return 0;
                        
                    case "server":
                        val = input.IndexOf(" ");
                        param = input.Substring(val + 1);
                        if (param.Equals("start"))
                        {
                            _serverSettings.LoadServerSettings();
                            _serverNet = new ServerNet(_serverSettings.Port);
                            if (_serverNet.OpenConnection())
                            {
                                Console.WriteLine("Server started succesfully on port " + _serverSettings.Port);
                            }
                            else Console.WriteLine("Server already started");
                        }
                        else if (param.Equals("stop"))
                        {
                            _serverNet.CloseConnection();
                            Console.WriteLine("Server has been stopped succesfully");
                        }
                        else if (param.Equals("restart"))
                        {
                            _serverNet.RestartConnection();
                            _serverSettings.LoadServerSettings();
                            _serverNet = new ServerNet(_serverSettings.Port);
                            Console.WriteLine("Server has been restarted succesfully");
                        }
                        else if (param.Equals("help"))
                        {
                            Console.WriteLine("\t server start   :to start server");
                            Console.WriteLine("\t server stop    :to stop server");
                            Console.WriteLine("\t server restart :to restart server");
                        }
                        else
                            Console.WriteLine("Invalid argument! Use start or stop");
                        break;
                    case "debug":
                        val = input.IndexOf(" ");
                        param = input.Substring(val + 1);
                        if (param.Equals("on"))
                        {
                            _serverSettings.DEBUG = true;
                            _serverSettings.SaveServerSettings();
                            Console.WriteLine("Succes");
                        }
                        else if (param.Equals("off"))
                        {
                            _serverSettings.DEBUG = false;
                            _serverSettings.SaveServerSettings();
                            Console.WriteLine("Succes");
                        }
                        else if (param.Equals("help"))
                        {
                            Console.WriteLine("\t debug on  :to activate debug messages");
                            Console.WriteLine("\t debug off :to dezactivate debug messages");

                        }

                        else
                            Console.WriteLine("Invalid argument! Use on or off");
                        break;
                    case "configure":
                        val = input.IndexOf(" ");
                        param = input.Substring(val + 1);
                        
                        if (val!=-1){
                            if (param.Equals("show"))
                            {
                                _serverSettings.LoadServerSettings();
                                Console.WriteLine("Port is " + _serverSettings.Port);
                            }
                            else 
                                if (param.Equals("help"))
                                {
                                    Console.WriteLine("\t configure <port> :for port listening");
                                    Console.WriteLine("\t configure show   :for printing port listening");
                                }
                            else
                            {
                                try
                                {
                                    port = Convert.ToInt32(param);

                                }
                                catch (Exception)
                                {
                                    Console.WriteLine("Invalid port! Only numeric caracters");
                                    break;
                                }
                                if ((port > 1024) && (port < 65335))
                                {
                                    _serverSettings.Port = port;
                                    _serverSettings.SaveServerSettings();
                                    Console.WriteLine("Succes");
                                }
                                else Console.WriteLine("Port should be between 1024 and 65000");
                            }
                        }
                        else Console.WriteLine("Invalid argument! Use configure <port>");
                        break;                    
                }
            }

        }
    }
}
